//随机生成大小写字母、数字互相组合指定长度的函数(type为num,eng,ENG,num_eng_ENG,num_eng,num_ENG,eng_ENG)
function randomCode(type, lenght) {
  //0~9的asc码为48~57，a~z的asc码为97~122，A~Z的asc码为65-90
  let code = ''
  for (let i = 0; i < lenght; i++) {
    let c = Math.round(Math.random() * 74) + 48
    if (type == 'num') {
      code += String.fromCharCode(Math.round(Math.random() * 9) + 48)
    } else if (type == 'eng') {
      code += String.fromCharCode(Math.round(Math.random() * 25) + 97)
    } else if (type == 'ENG') {
      code += String.fromCharCode(Math.round(Math.random() * 25) + 65)
    } else if (type == 'num_eng') {
      while (c > 57 && c < 97) {
        c = Math.round(Math.random() * 74) + 48
      }
      code += String.fromCharCode(c)
    } else if (type == 'num_ENG') {
      while (c > 57 && c < 65 || c > 90 && c < 122) {
        c = Math.round(Math.random() * 74) + 48
      }
      code += String.fromCharCode(c)
    } else if (type == 'eng_ENG') {
      while (c < 65 || c > 90 && c < 97) {
        c = Math.round(Math.random() * 74) + 48
      }
      code += String.fromCharCode(c)
    } else if (type == 'num_eng_ENG') {
      while (c > 57 && c < 65 || c > 90 && c < 97) {
        c = Math.round(Math.random() * 74) + 48
      }
      code += String.fromCharCode(c)
    } else {
      return '参数错误'
    }
  }
  return code //返回结果
}
// const equation = 8 * 60 * 60 * 1000 //时差
const equation = 0 //时差

//返回星期几
function getDay(timestamp) {
  let day
  if (timestamp) {
    day = new Date(timestamp).getDay()
  } else {
    day = new Date().getDay()
  }
  if (day == 0) day = '星期日'
  if (day == 1) day = '星期一'
  if (day == 2) day = '星期二'
  if (day == 3) day = '星期三'
  if (day == 4) day = '星期四'
  if (day == 5) day = '星期五'
  if (day == 6) day = '星期六'
  return day
}
// 获取年月日,place是否在前面补0
function getYMD(timestamp, place) {
  let y, m, d
  if (timestamp) {

    y = new Date(timestamp + equation).getFullYear()
    m = new Date(timestamp + equation).getMonth() + 1
    d = new Date(timestamp + equation).getDate()
    if (!place) m = m > 9 ? m : '0' + m
    if (!place) d = d > 9 ? d : '0' + d
  } else {
    y = new Date(Date.now() + equation).getFullYear()
    m = new Date(Date.now() + equation).getMonth() + 1
    d = new Date(Date.now() + equation).getDate()
    if (!place) m = m > 9 ? m : '0' + m
    if (!place) d = d > 9 ? d : '0' + d
  }
  return y + '-' + m + '-' + d
}
// 根据时间戳差换算为天数、小时、分钟数差
function timestamp_to_date(timestamp) {
  let days = Math.floor(timestamp / (1000 * 60 * 60 * 24))//计算天数
  let hours = Math.floor(timestamp / (1000 * 60 * 60)) % 24//计算小时
  let minutes = Math.floor(timestamp / (1000 * 60)) % 60//计算分钟
  return days + '天' + hours + '时' + minutes + '分'
}

// 获取年月日时分秒
function getYMDHMS(timestamp) {

  let y, m, d, h, min, s
  if (timestamp) {
    y = new Date(timestamp + equation).getFullYear()
    m = new Date(timestamp + equation).getMonth() + 1
    d = new Date(timestamp + equation).getDate()
    h = new Date(timestamp + equation).getHours()
    min = new Date(timestamp + equation).getMinutes()
    s = new Date(timestamp + equation).getSeconds()
    m = m > 9 ? m : '0' + m
    d = d > 9 ? d : '0' + d
    h = h > 9 ? h : '0' + h
    min = min > 9 ? min : '0' + min
    s = s > 9 ? s : '0' + s
  } else {
    y = new Date(Date.now() + equation).getFullYear()
    m = new Date(Date.now() + equation).getMonth() + 1
    d = new Date(Date.now() + equation).getDate()
    h = new Date(Date.now() + equation).getHours()
    min = new Date(Date.now() + equation).getMinutes()
    s = new Date(Date.now() + equation).getSeconds()
    m = m > 9 ? m : '0' + m
    d = d > 9 ? d : '0' + d
    h = h > 9 ? h : '0' + h
    min = min > 9 ? min : '0' + min
    s = s > 9 ? s : '0' + s
  }
  return y + '-' + m + '-' + d + ' ' + h + ':' + min + ':' + s
}

// 导出
module.exports = {
  randomCode,
  getDay,
  getYMD,
  timestamp_to_date,
  getYMDHMS,
}
